//
//  colorViewController.m
//  闪烁圆点动画
//
//  Created by Mac on 16/6/6.
//  Copyright © 2016年 liuyihao. All rights reserved.
//

#import "colorViewController.h"

@interface colorViewController () {
    NSMutableArray *_aniArr;
}

@end

@implementation colorViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd];
    btn.frame = CGRectMake(200, 200, 50, 50);
    [btn addTarget:self action:@selector(removeLayer:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    // Do any additional setup after loading the view.
}

- (void)startAnimationWithView:(UIView *)view {
    
    //创建执行动画的Layer
    
    CALayer*layer = [CALayer layer];
    
    layer.frame=CGRectMake(0,0, 30, 30);
    
    layer.cornerRadius= 30/2;
    
    layer.position = CGPointMake(view.bounds.size.width / 2, view.bounds.size.height / 2);
    
    layer.backgroundColor= [UIColor orangeColor].CGColor;
    [view.layer addSublayer:layer];
    
    //圆的大小改变
    
    CABasicAnimation*boundsAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale.xy"];
    
    boundsAnimation.fromValue= [NSNumber numberWithFloat:0];
    
    boundsAnimation.toValue= [NSNumber numberWithFloat:1];
    
    boundsAnimation.removedOnCompletion=YES;
    
    boundsAnimation.duration=1.0;
    
    //透明度的改变
    
    CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    
    opacityAnimation.fromValue= [NSNumber numberWithFloat:1];
    
    opacityAnimation.toValue= [NSNumber numberWithFloat:0];
    
    opacityAnimation.removedOnCompletion=YES;
    
    opacityAnimation.duration=1.0;
    
    //创建动画组
    
    CAAnimationGroup*group = [CAAnimationGroup animation];
    
    group.animations=@[boundsAnimation, opacityAnimation];
    
    group.duration=1.0;
    
    group.removedOnCompletion=YES;
    
    group.repeatCount = 10000;
    
    [layer addAnimation:group forKey:@"xiuyixiu"];
    
    //动画完成后移除Layer
    
//    [self performSelector:@selector(removeLayer:) withObject:layer afterDelay:3];
    
}

//移除Layer

- (void)removeLayer:(UIButton *)btn{
    
    
    for (UIView *view in _aniArr) {
        NSLog(@"%d", view.layer.sublayers.count);
        if (btn.selected) {
            [self startAnimationWithView:view];
        } else {
            [view.layer.sublayers[0] removeFromSuperlayer];
        }
    }
    btn.selected = !btn.selected;
}

//点击页面开始执行动画

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event{
    _aniArr = [NSMutableArray array];
    for (NSInteger i = 0; i < 3; i++) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(50 + 50 * i, 100 + 50 * i, 10, 10)];
        view.backgroundColor = [UIColor orangeColor];
        view.layer.cornerRadius = 5;
        [self.view addSubview:view];
        [_aniArr addObject:view];
        [self startAnimationWithView:view];
    }
    
}

//暂停layer上面的动画
- (void)pauseLayer:(CALayer*)layer
{
    CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
    layer.speed = 0.0;
    layer.timeOffset = pausedTime;
}


//继续layer上面的动画
- (void)resumeLayer:(CALayer*)layer
{
    CFTimeInterval pausedTime = [layer timeOffset];
    layer.speed = 1.0;
    layer.timeOffset = 0.0;
    layer.beginTime = 0.0;
    CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
    layer.beginTime = timeSincePause;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
